package com.xxkk.spring_boot_web.util;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

public class TimeUtil {
    private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("Asia/Shanghai");// 英国 Europe/London

    public static TimeZone getTimeZone(){
        return TIME_ZONE;
    }
    /**
     * 获取1970年到今天的天数（根据指定时区）
     * xxkk
     * @return 1970年到今天的天数
     */
    public static int getTodayIntValue() {
        Calendar calendar = Calendar.getInstance(TIME_ZONE); // 设置时区
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        long todayMillis = TimeUnit.MILLISECONDS.toDays(calendar.getTimeInMillis());
        long currentMillis = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis());

        return (int) (currentMillis - todayMillis);
    }

    /**
     * 获取指定时间的天数值（从1970年1月1日开始计算，根据指定时区）
     * xxkk
     * @param time 指定的时间戳
     * @return 指定时间的天数值
     */
    public static int getDateIntValue(long time) {
        Calendar calendar = Calendar.getInstance(TIME_ZONE); // 设置时区
        calendar.setTimeInMillis(time);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        long specifiedDateMillis = TimeUnit.MILLISECONDS.toDays(calendar.getTimeInMillis());
        long referenceDateMillis = TimeUnit.MILLISECONDS.toDays(new Date(0).getTime());

        return (int) (specifiedDateMillis - referenceDateMillis);
    }

    /**
     * 获取指定日期的天数值（从1970年1月1日开始计算，根据指定时区）
     * xxkk
     * @param date 指定的日期
     * @return 指定日期的天数值
     */
    public static int getDateIntValue(Date date) {
        Calendar calendar = Calendar.getInstance(TIME_ZONE); // 设置时区
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        long specifiedDateMillis = TimeUnit.MILLISECONDS.toDays(calendar.getTimeInMillis());
        long referenceDateMillis = TimeUnit.MILLISECONDS.toDays(new Date(0).getTime());

        return (int) (specifiedDateMillis - referenceDateMillis);
    }

    /**
     获取距离明天零点剩余的分钟数
     * xxkk
     @return 距离明天零点剩余的分钟数
     */
    public static int getNextDayZeroHourLeftMinute() {
    	Calendar todayZero = Calendar.getInstance(TIME_ZONE); // 设置时区
    	todayZero.set(Calendar.HOUR_OF_DAY, 0);
    	todayZero.set(Calendar.SECOND, 0);
    	todayZero.set(Calendar.MINUTE, 0);
    	todayZero.set(Calendar.MILLISECOND, 0);
        Calendar now = Calendar.getInstance();
        return (int) (TimeUnit.DAYS.toMinutes(1) - TimeUnit.MILLISECONDS.toMinutes(now.getTimeInMillis() - todayZero.getTimeInMillis()));
    }

    /**
     获取当前日期所属的周数
     * xxkk
     @return 当前日期所属的周数
     */
    public static int getWeekOfYear() {
    	int weekOfYear = -1;
    	Calendar cal = Calendar.getInstance(TIME_ZONE); // 设置时区
    	if(cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
    		weekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
    	} else {
    		weekOfYear = cal.get(Calendar.WEEK_OF_YEAR) - 1;
    		if(weekOfYear == 0) {
    			weekOfYear = 52;
    		}
    	}
    	
    	return weekOfYear;
    }

    /**
     获取当天零点的时间戳（毫秒）
     * xxkk
     @return 当天零点的时间戳
     */
    public static long getTodayZeroHourMill() {
        Calendar cal = Calendar.getInstance(TIME_ZONE);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     获取指定日期零点的时间戳（毫秒）
     * xxkk
     @param date 指定的日期
     @return 指定日期零点的时间戳
     */
    public static long getZeroHourMill(Date date) {
        Calendar cal = Calendar.getInstance(TIME_ZONE);
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     获取前n天零点的时间戳（毫秒）
     * xxkk
     @param beforeDay 前n天
     @return 前n天零点的时间戳
     */
    public static long getDayBeforeZeroHourMill(int beforeDay) {
        Calendar cal = Calendar.getInstance(TIME_ZONE);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis() - TimeUnit.DAYS.toMillis(beforeDay);
    }

    /**
     获取当前日期的天数（一年中的第几天）
     * xxkk
     @return 当前日期的天数
     **/
    public static int getCurrentDay() {
    	Calendar calendar = Calendar.getInstance(TIME_ZONE);
    	return (int)calendar.get(Calendar.DAY_OF_YEAR);
    }

    /**
     * 获取当前时间的整点时间
     * xxkk
     * @param hour
     * @return 时间戳
     */
    public static long getTodayAppointHourMill(int hour) {
        Calendar cal = Calendar.getInstance(TIME_ZONE);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    public static int getHourByDate() {
        Calendar calendar = Calendar.getInstance(TIME_ZONE);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }
    
    public static Date getTodayZeroHourDate() {
        Calendar cal = Calendar.getInstance(TIME_ZONE);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    public static Date getDate() {
        Calendar calendar = Calendar.getInstance(TIME_ZONE);
        return calendar.getTime();
    }
    
    public static Date getLastDayZeroHourDate() {
        return new Date(getDayBeforeZeroHourMill(1));
    }
    
    
    public static int getHourMinutes() {
        Calendar calendar = Calendar.getInstance(TIME_ZONE);
        return calendar.get(Calendar.MINUTE);
    }

    public static Date getMonthFirstDayZeroTime() {
        Calendar cal = Calendar.getInstance(TIME_ZONE);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

//    public static Date getLastMonthFirstDayZeroTime() {
//        Calendar cal = Calendar.getInstance(TIME_ZONE);
//        cal.add(Calendar.MONTH, -1);
//        cal.set(Calendar.HOUR_OF_DAY, 0);
//        cal.set(Calendar.SECOND, 0);
//        cal.set(Calendar.MINUTE, 0);
//        cal.set(Calendar.MILLISECOND, 0);
//        return cal.getTime();
//    }
//
//    public static Date getZeroTime() {
//        Calendar cal = Calendar.getInstance();
//        cal.add(Calendar.MONTH, -1);
//        cal.set(Calendar.HOUR_OF_DAY, 0);
//        cal.set(Calendar.SECOND, 0);
//        cal.set(Calendar.MINUTE, 0);
//        cal.set(Calendar.MILLISECOND, 0);
//        return cal.getTime();
//    }
    
    /**
     * 返回今天是星期几
     */
    public static int getWeekOfDate() { 
    	Date dt = new Date();
        Calendar cal = Calendar.getInstance(TIME_ZONE);
        cal.setTime(dt); 
        int weekOriginal = cal.get(Calendar.DAY_OF_WEEK) - 1; 
        if (weekOriginal < 0) 
            weekOriginal = 0; 
        
        return weekOriginal; 
    }
    
    public static int getWeekByDate(Date date) {
        Calendar cal = Calendar.getInstance(TIME_ZONE);
        cal.setTime(date); 
        int weekOriginal = cal.get(Calendar.DAY_OF_WEEK) - 1; 
        if (weekOriginal < 0) 
            weekOriginal = 0; 
        
        return weekOriginal; 
    }
    
//    public static long calSpecifiedHourMillisecond(final String hour) {
//    	long millisecond = 0l;
//		try {
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//			Calendar instance = Calendar.getInstance(TIME_ZONE);
//
//			int year = instance.get(Calendar.YEAR);
//			int month = instance.get(Calendar.MONTH) + 1;
//			int day = instance.get(Calendar.DAY_OF_MONTH);
//
//			StringBuffer sb = new StringBuffer();
//
//			sb.append(year).append("-").append(month).append("-").append(day).append(" ").append(hour);
//
//			Date date = sdf.parse(sb.toString());
//
//			millisecond = date.getTime();
//
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
//
//		return millisecond;
//    }
    
    public static int getQuarterHourLeftSecond() {
    	Calendar calendar = Calendar.getInstance();
    	int minute = calendar.get(Calendar.MINUTE);
    	int second = calendar.get(Calendar.SECOND);
    	if(minute < 15) {
    		return (15 - minute - 1) * 60 + 60 - second;
    	} else if(minute >= 15 && minute < 30){
    		return (30 - minute - 1) * 60 + 60 - second;
    	} else if(minute >= 30 && minute < 45) {
    		return (45 - minute - 1) * 60 + 60 - second;
    	} else {
    		return (60 - minute - 1) * 60 + 60 - second;
    	}
    }

}
